capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

/* 
Figure 1
*/

local data "" ;
local output "" ;

/* ******************************** */
/* **** PREP GENERAL POP STATS **** */
/* ******************************** */

input str11 pop percent21 percent25 percent30 ;
"_all"  .027 .048 .067 ;
"_1970" .028 .049 .067 ;
"_b_m"  .115 .192 .24 ; 
"_b_w"  .012 .029 .046 ;
"_w_m"  .028 .049 .065 ;
"_w_w"  .004 .010 .017 ;
end ;

reshape long percent, i(pop) j(age) ;

reshape wide percent, i(age) j(pop) string ;

tempfile fel_percent;
save `fel_percent' ;


/* ******************************************** */
/* **** PREP SUMM STAT DEMOGRAPHIC FIGURES **** */
/* ******************************************** */

/* Create data sets */
use crime_allssr_summstats_all, clear ;
keep *cum* n_crime2char_18plus ;
gen cat = "all" ;
tempfile all ;
save `all' ;

keep n_crime2any_cum* n_crime2char_cum* n_crime2fcvn_cum* ;
gen id = 1 ;
reshape long n_crime2any_cum n_crime2char_cum n_crime2fcvn_cum, i(id) j(age) ;
tempfile all_cum ;
save `all_cum' ;

use crime_allssr_summstats_diag, clear ;
keep diag *cum* n_crime2char_18plus ;
rename diag cat ;
tempfile diag ;
save `diag' ;

use crime_allssr_summstats_sex, clear ;
keep sex *cum* n_crime2char_18plus ;
rename sex cat ;
tempfile sex ;
save `sex' ;

use crime_allssr_summstats_singno, clear ;
keep max_fam_singno *cum* n_crime2char_18plus ;
rename max_fam_singno cat  ;
tostring cat, replace ;
replace cat = "twopar" if cat == "0" ;
replace cat = "singno" if cat == "1" ;
tempfile fam ;
save `fam' ;

use crime_allssr_summstats_parearn, clear ;
keep parhigh *cum* n_crime2char_18plus ;
rename parhigh cat  ;
tostring cat, replace ;
replace cat = "parlow" if cat == "0" ;
replace cat = "parhigh" if cat == "1" ;
tempfile earn ;
save `earn' ;

foreach race in white black nonw { ;
	use crime_allssr_summstats_`race', clear ;
	keep max_race_`race' *cum* n_crime2char_18plus ;
	rename max_race_`race' cat  ;
	tostring cat, replace ;
	replace cat = "non`race'" if cat == "0" ;
	replace cat = "`race'"    if cat == "1" ;
	tempfile `race' ;
	save ``race'' ;
} ;

use `all', clear ;
append using `diag' ;
append using `sex' ;
append using `fam' ;
append using `earn' ;
append using `white' ;
append using `black' ;
append using `nonw' ;

gen 	sort = 17 	if cat == "all" ;
replace sort = 15 	if cat == "twopar" ;
replace sort = 14 	if cat == "singno" ;
replace sort = 12 	if cat == "parhigh" ;
replace sort = 11 	if cat == "parlow" ;
replace sort = 9 	if cat == "white" ;
replace sort = 8 	if cat == "black" ;
replace sort = 6 	if cat == "F" ;
replace sort = 5 	if cat == "M" ;
replace sort = 3 	if cat == "nonment" ;
replace sort = 2 	if cat == "idment" ;
replace sort = 1 	if cat == "othment" ;

sort sort ;

twoway 
	(bar n_crime2char_18plus sort if cat == "all"		, horizontal color(gray))
	(bar n_crime2char_18plus sort if cat == "twopar"	, horizontal color(purple))
	(bar n_crime2char_18plus sort if cat == "singno"	, horizontal color(orange))
	(bar n_crime2char_18plus sort if cat == "parhigh"	, horizontal color(purple))
	(bar n_crime2char_18plus sort if cat == "parlow"	, horizontal color(orange))
	(bar n_crime2char_18plus sort if cat == "white"		, horizontal color(teal))
	(bar n_crime2char_18plus sort if cat == "black"		, horizontal color(olive))
	(bar n_crime2char_18plus sort if cat == "F"			, horizontal color(pink))
	(bar n_crime2char_18plus sort if cat == "M"			, horizontal color(blue))
	(bar n_crime2char_18plus sort if cat == "nonment"	, horizontal color(green))
	(bar n_crime2char_18plus sort if cat == "idment"	, horizontal color(green*.5))
	(bar n_crime2char_18plus sort if cat == "othment"	, horizontal color(orange))
	, 
	legend(off)
	ysc(r(0.5 17.5)) ylabel(1 "Mental" 2 "Intellectual" 3 "Physical" 5 "Male" 6 "Female" 8 "Black" 9 "White" 
							11 "Low parent earn" 12 "High parent earn" 14 "Single/no parent" 15 "Two parents" 17 "Overall", labsize(vsmall)) ytitle("") 
	xsc(r(0 .4)) xlab(0(.1).4) xtitle("Fraction with any adult criminal charges", size(medsmall))
	graphregion(color(white))
	bgcolor(white)
	ylabel(,nogrid angle(0))
	plotregion(margin(0 2 2 2))
	;
	
	graph export "`output'/fig1_a.png", width(1000) height(750) replace ;
	pause ;


/* *********************************************** */
/* **** BY YOB, WITH GENERAL POPULATION STATS **** */
/* *********************************************** */

clear ;
set obs 21 ;
gen age = _n + 18 ;
merge 1:1 age using `all_cum' ;
drop _merge ;

merge 1:1 age using `fel_percent' ;
drop _merge ;

twoway 
	(connected n_crime2fcvn_cum age if age >= 20 & age <= 38		, color(orange))
	(connected percent_1970 age	    if age >= 20 & age <= 38		, msymbol(T) color(blue))
	, 
	legend(off)
	title("Any felony conviction", size(medsmall))
	text(0.23 38 "SSI", size(vsmall))
	text(0.07 31.5 "General pop" "(YOB 1970s)", size(vsmall))
	xsc(r(20 38)) xlab(20(2)38)
	xtitle("Age", size(medsmall)) 
	ysc(r(0 .25)) ylab(0(.05).25) ytitle("Fraction with any felony conviction", size(medsmall))
	graphregion(color(white))
	bgcolor(white)
	ylabel(,nogrid angle(0))
	;
	graph export "`output'/fig1_b.png", width(1000) height(750) replace ;
